HGE:Tutorials:Audio
=Sound Effects= For sound effects, the following audio file formats are supported: WAV, MP3, MP2, MP1 and OGG. =Defining sound effects in the resource manager= Here is a sample sound effect you can use for this tutorial: Media:HGE_click.ogg Make sure to place it in the current directory and name it click.ogg To define a sound effect, give it a name and specify the file name of the sound using the filename parameter. Add the following to the resource.res file: Sound click { filename=click.ogg } =Channels= Audio channels are a way to separate sounds and music for different purposes. For instance: You may want to have a channel that just plays all enemy sound effects, another channel just for player sound effects and so on. Let's declare 2 channels at the top of the program: HCHANNEL chan2; Channel 0 will be used for playing music and channel 1 will be for playing sound effects. =Playing sound effects= In this tutorial we'll play a sound whenever the user clicks the left mouse button. Declare a sound effect at the top of the program: HEFFECT mySound; The HEFFECT type signifies that this is a handle to a sound effect. Initialize the sound effect in WinMain, beneath the other initializations: mySound = myRes->GetEffect("click"); This retrieves the sound effect from the resource script file. In the FrameFunc, below the player movement functions, add: //when left mouse is clicked, play sound effect if(hge->Input_GetKey() HGEK_LBUTTON) chan1 = hge->Effect_Play(mySound); The Effect_Play() function takes a single parameter: the sound effect that we want to play. It returns the channel in which you want to play the sound effect. chan1 in the above statement means that we want to play the sound through audio channel 1. Run the program, and if you press the left mouse button, the sound effect will be played through audio channel 1. =Music= There are 2 kinds of music formats that HGE uses: Stream and Music. :Stream: Supports MP1, MP2, MP3, OGG and WAV file formats. :Music: Supports MO3, IT, XM, S3M, MTM, MOD and UMX file formats. =Defining music in the resource manager= Here is a sample music file: Media:HGE_music.ogg Name it music.ogg and place it in the current directory. For bandwidth considerations, the music file is very short (only a few seconds long) but it demonstrates the purpose. Defining music is similar to defining sound effects. Be sure to use the appropriate keyword, either Stream or Music. In this case, we use Stream since it is an .ogg file. Add the following to the resource.res file: Stream theme { filename=music.ogg } =Playing music= In the following example, music will be played as soon as the program starts up. Declare a stream at the top of the program: HSTREAM myMusic; Initialize the sound effect in WinMain, beneath the other initializations: myMusic = myRes->GetStream("theme"); This retrieves the music from the resource script file. Right below the initialization in WinMain, play the music by adding this statement: chan0 = hge->Stream_Play(myMusic, true); //start playing music with looping The Stream_Play() function takes 2 parameters: the music that we want to play and whether to loop the music. Setting looping to true will play back the music from the beginning whenever it ends. It returns the channel in which you want to play the music. chan0 in the above statement means that we want to play the music through audio channel 0. Run the program and you will heard a short, repeating music clip being played. You can download the source file and resource script file for this tutorial here: Tutorial 6 source, Resource script Next Section: Particle effects Category:C Category:HGE